###############################
# Figures A1 and Table A8-A10
###############################

rm(list=ls())

# load packages 
library(foreign)
library(lmtest)
library(sandwich)
library(stargazer)
library(msm)
library("readxl")
library(cregg)
library("ggplot2")
library(gridExtra)
library(readstata13)

##############################
# Prepare data
##############################

# load data
load("replication_data.Rdata")
names(d)

#######################################
# AMCE by Bureaucrat Gender
#######################################

amces4a <- cj(d_match, outcome_q3 ~ speed_eng + theft_eng  + partyid_eng + age_eng + education_eng, id = ~idnum, estimate = "amce", by = ~gender_eng)[c(1:4,14:17),]
amces4a$feature = c("Speed corruption", "Speed corruption", "Theft corruption", "Theft corruption","Speed corruption", "Speed corruption", "Theft corruption", "Theft corruption")
diff_amces4a <- cj(d_match, outcome_q3 ~ speed_eng + theft_eng + partyid_eng + age_eng + education_eng, id = ~idnum, estimate = "amce_diff", by = ~gender_eng)[7:8,]
diff_amces4a$feature = c("Speed corruption", "Theft corruption")
plot4a = plot(rbind(amces4a, diff_amces4a)) + ggplot2::facet_wrap(~BY, ncol = 3L) + ggtitle("") + theme(plot.title = element_text(hjust = 0.5)) + geom_point(size = 1.5) + scale_y_discrete(labels=c("(Speed corruption)"="SPEED CORRUPTION","(Theft corruption)"="THEFT CORRUPTION"))

# Figure A1
ggsave('figureA1.pdf', plot4a, width = 10, height = 4)

# Tables A8-A10
cj(d_match, outcome_q3 ~ speed_eng + theft_eng  + partyid_eng + age_eng + education_eng, id = ~idnum, estimate = "amce", by = ~gender_eng)
cj(d_match, outcome_q3 ~ speed_eng + theft_eng + partyid_eng + age_eng + education_eng, id = ~idnum, estimate = "amce_diff", by = ~gender_eng)

